package com.ruoyi.web.ydz.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.ydz.domain.UserDocument;
import com.ruoyi.web.ydz.mapper.UserDocumentMapper;
import com.ruoyi.web.ydz.service.IUserDocumentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 用户个人资料Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class UserDocumentServiceImpl implements IUserDocumentService
{
    @Resource
    private UserDocumentMapper userDocumentMapper;

    @Resource
    private ISysUserService userService;

    @Resource
    private TokenService tokenService;

    /**
     * 查询用户个人资料
     * 
     * @param id 用户个人资料主键
     * @return 用户个人资料
     */
    @Override
    public UserDocument selectUserDocumentById(Long id)
    {
        return userDocumentMapper.selectUserDocumentById(id);
    }

    /**
     * 查询用户个人资料列表
     * 
     * @param userDocument 用户个人资料
     * @return 用户个人资料
     */
    @Override
    public List<UserDocument> selectUserDocumentList(UserDocument userDocument)
    {
        return userDocumentMapper.selectUserDocumentList(userDocument);
    }

    /**
     * 新增用户个人资料
     * 
     * @param userDocument 用户个人资料
     * @return 结果
     */
    @Override
    public int insertUserDocument(UserDocument userDocument)
    {

        LoginUser loginUser = getLoginUser();
        SysUser currentUser = loginUser.getUser();

        /*修改用户昵称 用户性别*/
        if (StringUtils.isNotEmpty(userDocument.getUserName())){
            currentUser.setNickName(userDocument.getUserName());
            currentUser.setSex(userDocument.getSexType());
            if (userService.updateUserProfile(currentUser) > 0)
            {
                // 更新缓存用户信息
                tokenService.setLoginUser(loginUser);
            }
        }

        userDocument.setCreateTime(DateUtils.getNowDate());
        userDocument.setCreateBy(currentUser.getUserName());
        userDocument.setCreateName(currentUser.getNickName());
        userDocument.setUserId(currentUser.getUserId());

        return userDocumentMapper.insertUserDocument(userDocument);
    }

    /**
     * 修改用户个人资料
     * 
     * @param userDocument 用户个人资料
     * @return 结果
     */
    @Override
    public int updateUserDocument(UserDocument userDocument)
    {
        LoginUser loginUser = getLoginUser();
        SysUser currentUser = loginUser.getUser();

        /*修改用户昵称 用户性别*/

        if (StringUtils.isNotEmpty(userDocument.getUserName()) || StringUtils.isNotEmpty(userDocument.getSexType())){
            if (StringUtils.isNotEmpty(userDocument.getUserName())) currentUser.setNickName(userDocument.getUserName());
            if (StringUtils.isNotEmpty(userDocument.getSexType())) currentUser.setSex(userDocument.getSexType());

            if (userService.updateUserProfile(currentUser) > 0)
            {
                // 更新缓存用户信息
                tokenService.setLoginUser(loginUser);
            }
        }
        userDocument.setUserId(currentUser.getUserId());
        userDocument.setUpdateTime(DateUtils.getNowDate());
        userDocument.setUpdateBy(currentUser.getUserName());
        userDocument.setUpdateName(currentUser.getNickName());
        return userDocumentMapper.updateUserDocument(userDocument);
    }

    /**
     * 批量删除用户个人资料
     * 
     * @param ids 需要删除的用户个人资料主键
     * @return 结果
     */
    @Override
    public int deleteUserDocumentByIds(Long[] ids)
    {
        return userDocumentMapper.deleteUserDocumentByIds(ids);
    }

    /**
     * 删除用户个人资料信息
     * 
     * @param id 用户个人资料主键
     * @return 结果
     */
    @Override
    public int deleteUserDocumentById(Long id)
    {
        return userDocumentMapper.deleteUserDocumentById(id);
    }
}
